﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL;
using System.IO;
using System.Web.Script.Serialization;
using System.Data;

namespace website.RoleInfo
{
    /// <summary>
    /// RoleInfo 的摘要说明
    /// </summary>
    public class RoleInfo : IHttpHandler
    {
        string Action = "";

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            Action = RequstString("Action");
           
            if (Action == "initCustomerInfo")
            {
                context.Response.Write(initCustomerInfo());
            }

            else if (Action == "GetRoleInfo")
            {
                string sidx = RequstString("sidx");
                string sord = RequstString("sord");
                string keyvalue = RequstString("keyword");
                string CustomerName = RequstString("CustomerName");
                context.Response.Write(getRoleInfo(sidx,sord,keyvalue,CustomerName));
            }
            else if (Action == "deleteRoleInfo")
            {
                context.Response.Write(deleteRoleInfo());
            }
            else if (Action == "getRoleInfoByID")
            {
                string RoleID = RequstString("RoleID");
                context.Response.Write(getRoleInfoByID(RoleID));
            }

            else
            {
                var stream = new StreamReader(context.Request.InputStream).ReadToEnd();
                var msgdata = new JavaScriptSerializer().Deserialize<MSG>(stream);
               
                //保存新增角色
                if (msgdata.Action == "addRoleInfo")
                {
                    context.Response.Write(addRoleInfo(msgdata.RoleName, msgdata.CustomName, msgdata.Remark, msgdata.quanxian));
                }
                //保存编辑角色
                else if (msgdata.Action == "editRoleInfo")
                {
                    var editroledata = new JavaScriptSerializer().Deserialize<EditRole>(stream);
                    context.Response.Write(editRoleInfo(editroledata.RoleID, editroledata.RoleName,  editroledata.Remark, editroledata.quanxian));
                }
            }
        }

        public string initCustomerInfo()
        {
            string CustomerName = RequstString("CustomerName");
            string json = RoleInfoBLL.initCustomerInfo(CustomerName);
            return json;
        }

        /// <summary>
        /// 保存新增角色
        /// </summary>
        /// <param name="RoleName"></param>
        /// <param name="CustomName"></param>
        /// <param name="Remark"></param>
        /// <param name="quanxian"></param>
        /// <returns></returns>
        public string addRoleInfo(string RoleName, string CustomName, string Remark, List<string> quanxian)
        {
            string json = RoleInfoBLL.addRoleInfo(RoleName,CustomName,Remark,quanxian);
            return json;
        }

        public string editRoleInfo(string RoleID, string RoleName,  string Remark, List<string> quanxian)
        {
            string json = RoleInfoBLL.editRoleInfo(RoleID, RoleName, Remark, quanxian);
            return json;
        }


        public string getRoleInfo(string sidx, string sord, string keyvalue, string CustomerName)
        {
            string strJson = "";
            DataTable dt = RoleInfoBLL.getRoleInfo(sidx, sord, keyvalue, CustomerName);

            if (dt != null)
            {
                string page = RequstString("page");
                //String page =Re .getParameter("page"); // 取得当前页数,注意这是jqgrid自身的参数 
                string rows = RequstString("rows");  // 取得每页显示行数，,注意这是jqgrid自身的参数 
                int totalRecord = dt.Rows.Count; // 总记录数(应根据数据库取得，在此只是模拟) 
                int totalPage = totalRecord % Convert.ToInt16(rows) == 0 ? totalRecord
                / Convert.ToInt16(rows) : totalRecord / Convert.ToInt16(rows)
                + 1; // 计算总页数 
                int index = (Convert.ToInt16(page) - 1) * Convert.ToInt16(rows); // 开始记录数 
                int pageSize = Convert.ToInt16(rows);
                strJson = "{\"page\":" + page + ",\"total\": " + totalPage + "  ,\"records\":" + dt.Rows.Count.ToString() + ",\"rows\":[";
                for (int j = index; j < pageSize + index && j < totalRecord; j++)
                {
                    strJson += "{";
                    strJson += "\"id\":\"" + (j + 1).ToString() + "\",";
                    strJson += "\"cell\":";

                    strJson += "[";
                    strJson += "\"" + dt.Rows[j]["ID"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["RoleName"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["CustomName"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["Remark"].ToString() + "\"";
                    strJson += "]";
                    strJson += "}";
                    if (j != pageSize + index - 1 && j != totalRecord - 1)
                    {
                        strJson += ",";
                    }
                }
            }
            else
            {
                strJson = "{\"page\":1,\"total\":0,\"records\":0,\"rows\":[";

            }
            strJson = strJson.Trim().TrimEnd(new char[] { ',' });
            strJson += "]}";
            return strJson;
        }

        /// <summary>
        /// 根据ID找到角色信息
        /// </summary>
        /// <param name="RoleID"></param>
        /// <returns></returns>
        public string getRoleInfoByID(string RoleID)
        {
            string json = RoleInfoBLL.getRoleInfoByID(RoleID);
            return json;
        }

        public string deleteRoleInfo()
        {
            string RoleID = RequstString("RoleID");

            string json = RoleInfoBLL.deleteRoleInfo(RoleID);

            return json;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        public static string RequstString(string sParam)
        {
            return (HttpContext.Current.Request[sParam] == null ? string.Empty
                  : HttpContext.Current.Request[sParam].ToString().Trim());
        }

        //定义一个类用来将前端传递过来的数据进行反序列化  
        public class MSG
        {
            public string Action { set; get; }
            public string RoleName { set; get; }
            public string CustomName { set; get; }
            public string Remark { set; get; }
            public List<string> quanxian { set; get; }

        }


        public class EditRole
        {
            public string Action { set; get; }
            public string RoleID { set; get; }
            public string RoleName { set; get; }
            public string Remark { set; get; }
            public List<string> quanxian { set; get; }

        }  
    }
}